//
//  BarnnerView.m
//  yixintong
//
//  Created by longzhiyuan on 15/6/8.
//
//
//#import "KeyboardViewController.h"
#import "BannerView.h"
#import "UIImageView+WebCache.h"
//#import "BaifangViewController.h"
#define IPHONE_W ([[UIScreen mainScreen] bounds].size.width)
//#define IPHONE_H ([[UIScreen mainScreen]bounds].size.height)
//
@implementation BannerView

-(void)initData:(NSMutableArray*)array imgBounds:(CGRect)bounds
{
    
    //NSArray *array = [[NSArray alloc]initWithObjects:@"http://ad.28fax.com:8008/data/ad/h1z/h2z/781430305195_320.png",@"http://ad.28fax.com:8008/data/ad/h1z/h2z/671430305315_320.png", nil];
    //
    array = [[NSArray alloc]initWithObjects:@"http://www.dui8.net/public/attachment/201506/201506150856308556.jpg",@"http://www.dui8.net/public/attachment/201506/201506150856189126.jpg", @"http://www.dui8.net/public/attachment/201506/201506150856189126.jpg",nil];
    //
    [self clearAllImg];
    self.totalPage = array.count;
    self.bgScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_W,bounds.size.height)];
    self.bgScroll.userInteractionEnabled=YES;
    self.bgScroll.scrollEnabled=YES;
    self.bgScroll.delegate=self;
    self.bgScroll.pagingEnabled=YES;
    self.bgScroll.showsHorizontalScrollIndicator=NO;
    self.bgScroll.showsVerticalScrollIndicator=NO;
    self.bgScroll.contentSize=CGSizeMake(IPHONE_W* self.totalPage,0);
    [self addSubview:self.bgScroll];
    for (int i=0; i<self.totalPage; i++)
    {
        NSString *nsStrUrl = array[i];
        UIImageView* subview=[[UIImageView alloc]initWithFrame:CGRectMake(IPHONE_W*i, 0, IPHONE_W,bounds.size.height)];
        NSURL *nsUrl = [NSURL URLWithString:nsStrUrl];
        //NSData *nsdata = [NSData dataWithContentsOfURL:nsUrl];
        //[subview setImage:[[UIImage alloc]initWithData:nsdata]];
        [subview sd_setImageWithURL:nsUrl placeholderImage:[UIImage imageNamed:@"nopic.png"]];
        [self.bgScroll addSubview:subview];
        [subview setUserInteractionEnabled:YES];
        [subview setTag:i];
        [subview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBannerImg:)]];
        //[subview setObservationInfo:CFBridgingRetain([NSString stringWithFormat:@"%d",i])];
    }
    if (self.totalPage>1)
    {
        self.pageCtrl=[[UIPageControl alloc]initWithFrame:CGRectMake(bounds.size.width/2+(self.totalPage*15)/2, bounds.size.height-50,self.totalPage*15, 30)];
        self.pageCtrl.numberOfPages=self.totalPage;
        self.pageCtrl.currentPage=0;
        // 设置选中页的圆点颜色
        if ([[[UIDevice currentDevice]systemVersion]floatValue]>=6.0)
        {
            self.pageCtrl.currentPageIndicatorTintColor=[UIColor purpleColor];
            // 设置非选中页的圆点颜色
            self.pageCtrl.pageIndicatorTintColor=[UIColor grayColor];
        }
        [self.pageCtrl addTarget:self action:@selector(pageCtrl_click:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:self.pageCtrl];
        self.pageCtrl.numberOfPages=self.totalPage;
        [self.pageCtrl setCurrentPage:0];
    }
    self.blnInitData=YES;
    
}
-(void)clearAllImg
{
    NSArray *arr=self.bgScroll.subviews;
    for (NSInteger i=0; i<arr.count; i++)
    {
        UIImageView* subview=(UIImageView* )arr[i];
        if (subview)
        {
            [subview removeFromSuperview];
        }
        //NSLog(@"subview===%lu",(unsigned long)self.bgScroll.subviews.count);
    }
}
-(void)pause:(BOOL)bln
{
    if (bln)
    {
        //关闭定时器
        [self.timer setFireDate:[NSDate distantFuture]];
    }else{
        //开启定时器
        [self.timer setFireDate:[NSDate distantPast]];
    }
}
-(void)start
{
    if (self.totalPage>1)
    {
        NSLog(@"===幻灯手动后滚动===");
        //幻灯手动后滚动
        self.timer = [NSTimer scheduledTimerWithTimeInterval:8.8f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
    }
}
-(void)stop
{
    if (self.timer)
    {
        [self.timer invalidate];
        self.timer=nil;
    }
}
- (void)scrollToNextPage:(id)sender
{ //切换到下一张
    NSUInteger currentPage= self.pageCtrl.currentPage;
    if (currentPage==self.totalPage-1)
    {
        directions = NO;
    } else if (currentPage == 0)
    {
        directions = YES;
    }
    if (directions)
    {
        currentPage++;
    } else {
        currentPage--;
    }
    [self.pageCtrl setCurrentPage:currentPage];
    CGRect frame=self.bgScroll.frame;
    frame.origin.x = self.bgScroll.frame.size.width*currentPage;
    //NSLog(@"=======scrollToNextPage=====%d",self.totalPage);
    //幻灯切换宽度
    [self.bgScroll setContentOffset:frame.origin animated:YES];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat pageWidth =self.bgScroll.frame.size.width;
    int page = floor((self.bgScroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageCtrl.currentPage = page;

}

- (IBAction)pageCtrl_click:(UIPageControl*)sender
{
    //NSLog(@"===pageCtrl_click==%ld",(long)sender.currentPage);
    NSUInteger currentPage = sender.currentPage;
    //幻灯切换宽度
    CGRect frame=self.bgScroll.frame;
    frame.origin.x = self.bgScroll.frame.size.width*currentPage;
    [self.bgScroll setContentOffset:frame.origin animated:YES];
}
-(void)clickBannerImg:(UITapGestureRecognizer *)gestureRecognizer
{
    UIView *viewClicked=[gestureRecognizer view];
    //NSLog(@"==clickBannerImg===%@",[viewClicked observationInfo]);
    //NSString* urlStr=[[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"urlAdd%d",[viewClicked observationInfo]]];
    //urlStr=@"http://baidu.com";
    if (self.delegate)
    {
        [self.delegate selectBannerImg:[NSString stringWithFormat:@"%ld",(long)viewClicked.tag]];
    }
}
-(void)dealloc
{
    [self clearAllImg];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
